﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KSWSClient.Code.IService;
using KSWSClient.Code.Service;
using KSWSClient.Code.Model;

namespace KSWSClient.ConfigUI
{
    public partial class SMConfigPanel : UserControl
    {
        public SMConfigPanel()
        {
            InitializeComponent();
            InitForm();
        }
        // 直接初始化
        private IConfigService configService = new ConfigService();
        // 初始化表单
        private void InitForm()
        {
            ConfigModel config = configService.GetConfig();
            this.txtServerHost.Text = config.ServerHost;
            this.txtServerPort.Text = config.ServerPort;
            this.txtContextPath.Text = config.ContextPath;
            this.txtUserName.Text = config.UserName;
            this.txtPassword.Text = config.Password;
        }
        // 根据表单数据获取一个ConfigModel方便保存
        private ConfigModel GetModelFromForm()
        {
            ConfigModel config = new ConfigModel();
            config.ServerHost = this.txtServerHost.Text;
            config.ServerPort = this.txtServerPort.Text;
            config.ContextPath = this.txtContextPath.Text;
            config.UserName = this.txtUserName.Text;
            config.Password = this.txtPassword.Text;
            return config;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ConfigModel config = GetModelFromForm();
            this.configService.SaveConfig(config);
            MessageBox.Show(this, "Save the configuration successfully！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            InitForm();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            InitForm();
        }
    }
}
